package com.xinghai.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinghai.system.domain.SysNotice;
import com.xinghai.system.mapper.SysNoticeMapper;
import com.xinghai.system.service.ISysDictDataService;
import com.xinghai.system.service.ISysNoticeService;
import org.assertj.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 公告 服务层实现
 * 
 * @author xinghai
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService
{
    @Autowired
    private SysNoticeMapper noticeMapper;
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询公告信息
     * 
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     * 
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice)
    {
        return noticeMapper.insertNotice(notice);
    }

    @Override
    public int addNotice(SysNotice notice) {
        notice.setNoticeType(StringPool.ONE);
        notice.setStatus(StringPool.ZERO);
        notice.setCreateTime(DateUtil.now());
        notice.setUpdateTime(DateUtil.now());
        notice.setUpdateBy(notice.getCreateBy());
        Integer companyType = notice.getCompanyType();
        if(Objects.isNull(companyType)){
            throw new RuntimeException("请填写需要通知的公司类型!");
        }
        String redUrl = notice.getRedUrl();
        if(Objects.isNull(redUrl)){
            throw new RuntimeException("请填写需要跳转的通知地址!");
        }
        notice.setRedUrl(dictDataService.selectDictLabel("red_url", redUrl));
        return noticeMapper.insert(notice);
    }

    /**
     * 修改公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice)
    {
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     * 
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId)
    {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     * 
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds)
    {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }


    /**
     * 根据实例id查询消息
     * @param instanceId
     * @return
     */
    @Override
    public SysNotice getSysNoticeByInstanceId(String instanceId) {
        return noticeMapper.selectOne(Wrappers.<SysNotice>lambdaQuery().eq(SysNotice::getInstanceId, instanceId).eq(SysNotice::getStatus,0).orderByAsc(SysNotice::getCreateTime));
    }
}
